#include <QtGui/QListWidget>
#include <QtGui/QStackedWidget>
#include <QtGui/QScrollBar>
#include <QtGui/QVBoxLayout>
#include "TabsView.h"

TabsView::TabsView(QWidget *parent) : QWidget(parent) {
	listWidget = new QListWidget;
	listWidget->setFixedHeight(64);
	listWidget->setIconSize(QSize(40,40));
	listWidget->setViewMode(QListView::IconMode);
	listWidget->setMovement(QListView::Static);
	listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	listWidget->setFlow(QListView::LeftToRight);
	listWidget->setGridSize(QSize(80,80));
	listWidget->setWrapping(true);
	
	stackedWidget = new QStackedWidget;
	QColor backgroundColor = stackedWidget->palette().color(QPalette::Window);
	listWidget->setStyleSheet(QString("QListWidget { margin-top: 5px; background-color: rgb(%1,%2,%3,0); border:0px;} QListWidget:QListWidgetItem { margin-left:3px; }").arg(backgroundColor.red()).arg(backgroundColor.green()).arg(backgroundColor.blue()));
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->setContentsMargins(0,0,0,0);
	mainLayout->addWidget(listWidget);
	mainLayout->addWidget(stackedWidget);
	setLayout(mainLayout);
	
	connect(listWidget,SIGNAL(currentRowChanged(int)),this,SLOT(itemChanged(int)));
}

TabsView::~TabsView() {
	delete listWidget;
	delete stackedWidget;
}

void TabsView::addTab(QWidget *tabWidget,const QIcon& tabIcon,const QString& tabText) {
	QListWidgetItem *newItem = new QListWidgetItem;
	newItem->setText(tabText);
	newItem->setIcon(tabIcon);
	
	stackedWidget->addWidget(tabWidget);
	listWidget->addItem(newItem);
	
	setMinimumWidth(listWidget->count()*70);
}

void TabsView::itemChanged(int currentIndex) {
	stackedWidget->setMaximumHeight(stackedWidget->widget(currentIndex)->sizeHint().height());
	stackedWidget->setCurrentIndex(currentIndex);
	emit (tabChanged(stackedWidget->currentWidget()));
}

void TabsView::setCurrentWidget(QWidget *widget) {
	listWidget->setCurrentRow(stackedWidget->indexOf(widget));
}
